package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.itheima.dto.AdminChannelDto;
import com.itheima.mapper.ChannelMapper;
import com.itheima.po.AdminChannel;
import com.itheima.service.ChannelService;
import com.itheima.vo.AdminChannelVo;
import com.itheima.vo.PagingQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2023-01-04 15:22
 * description :
 */
@Service
public class ChannelServiceImpl implements ChannelService {
    @Autowired
    private ChannelMapper channelMapper;

    @Override
    public PagingQueryVo<AdminChannelVo> selectPage(AdminChannelDto dto) {
        Integer size = dto.getSize();
        int start=(dto.getPage()-1)*size;
        String name = dto.getName();
        MPJLambdaWrapper<AdminChannel> channelWrapper=new MPJLambdaWrapper<>();
        MPJLambdaWrapper<AdminChannel> countWrapper=new MPJLambdaWrapper<>();

        if (!StringUtils.isEmpty(name)){
            channelWrapper.like(AdminChannel::getName,name);
            countWrapper.like(AdminChannel::getName,name);
        }

        String last = "limit "+start+","+size;
        channelWrapper.last(last);
        List<AdminChannel> adminChannels = channelMapper.selectList(channelWrapper);

        PagingQueryVo<AdminChannelVo> pagingQueryVo = new PagingQueryVo<>();
        if (!CollectionUtils.isEmpty(adminChannels)){
            pagingQueryVo.setData(BeanUtil.copyToList(adminChannels, AdminChannelVo.class));
        }
        pagingQueryVo.setCurrentPage(dto.getPage());
        pagingQueryVo.setSize(size);
        Long count = channelMapper.selectCount(countWrapper);
        pagingQueryVo.setTotal(Integer.parseInt(String.valueOf(count)));
        return pagingQueryVo;
    }

    @Override
    public Boolean deleteChannel(Long id) {
        int delete = channelMapper.deleteById(id);
        return delete==1;
    }

    @Override
    public Boolean saveChannel(AdminChannelDto dto) {
        MPJLambdaWrapper<AdminChannel> selectWrapper=new MPJLambdaWrapper<>();
        selectWrapper.eq(AdminChannel::getName,dto.getName());
        AdminChannel adminChannelInDB = channelMapper.selectOne(selectWrapper);
        if (!ObjectUtils.isEmpty(adminChannelInDB)){
            throw new RuntimeException("需要添加的频道名已存在");
        }
        if (dto.getOrd()>255){
            throw new RuntimeException("排序数字过大");
        }
        AdminChannel adminChannel = new AdminChannel();
        BeanUtil.copyProperties(dto,adminChannel);
        if (dto.getStatus()){
            adminChannel.setStatus(1);
        }else {
            adminChannel.setStatus(0);
        }
        adminChannel.setId(IdUtil.getSnowflakeNextId());
        adminChannel.setCreateTime(LocalDateTime.now());
        adminChannel.setIsDefault(1);
        int insert = channelMapper.insert(adminChannel);
        return insert==1;
    }

    @Override
    public Boolean updateChannel(AdminChannelDto dto) {
        AdminChannel adminChannelInDB = channelMapper.selectById(dto.getId());
        if (ObjectUtils.isEmpty(adminChannelInDB)){
            throw new RuntimeException("需要修改的频道不存在");
        }
        AdminChannel adminChannel = new AdminChannel();
        Integer ord = dto.getOrd();
        if (!ObjectUtils.isEmpty(ord)&&ord>255){
            throw new RuntimeException("排序数字过大");
        }
        BeanUtil.copyProperties(dto,adminChannel);
        Boolean status = dto.getStatus();
        if (!ObjectUtils.isEmpty(status)){
            if (status){
                adminChannel.setStatus(1);
            }else {
                adminChannel.setStatus(0);
            }
        }

        int update = channelMapper.updateById(adminChannel);
        return update==1;
    }
}
